import os
import sys

from redis_util import RedisUtil

from spider_taobao_goods_detail import get_goods_detail_by_itemurl_sellerid

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
print(BASE_DIR)

import redis

from celery import Celery

app = Celery('insert_redis', broker='redis://127.0.0.1:6379', backend='redis://127.0.0.1:6379')

redis_key = "goods_items_url_list"


@app.task(time_limit=5)
def insert_redis():
    r = redis.StrictRedis(decode_responses=True)
    one_items_url = r.rpop(redis_key)
    if one_items_url:
        # 如果有这个url，则执行操作
        # 获取详情图片
        rdu = RedisUtil()
        if not rdu.get_list(one_items_url):
            # 如果获取不到这个结果，说明redis中没有这个值，需要重新抓取
            imgs_list = get_goods_detail_by_itemurl_sellerid(one_items_url)
            rdu.set_list(one_items_url, imgs_list)
        return one_items_url
